package com.example.utils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 异常处理类
 */
public class LoggingFilter implements Filter {

    private static final Logger logger = Logger.getLogger(LoggingFilter.class.getName());

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        try {
            // 执行下一个过滤器，或者目标资源
            filterChain.doFilter(servletRequest, servletResponse);
        } catch (Throwable t) {
            // 捕获异常并打印堆栈信息
            logger.log(Level.SEVERE, "Exception caught in LoggingFilter", t);
            throw t;
        }
    }

    @Override
    public void destroy() {
    }
}
